﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class User_UserEmail_EmailContent : System.Web.UI.Page
{
    private int emailId;
    private string type;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["type"] != null)
        {
            this.type = Request["type"];
        }
        else
        {
            this.type = "incept";
        }
        if (null != Request["eid"])
        {
            this.emailId = int.Parse(Request["eid"]);
            EmpEmailMappingtbService empEmailMappingtbService = new EmpEmailMappingtbService();
            empEmailMappingtbService.updateEmailReadState(emailId, type);
        }
        this.emailInfoShow();
    }

    protected void emailInfoShow()
    {
        if (emailId != 0)
        {
            EmpEmailMappingtbService empEmailMappingtbService = new EmpEmailMappingtbService();
            EmpEmailMappingtb empEmailMapping = empEmailMappingtbService.findByEmpEmailId(this.emailId, type);
            this.lblSendDateTime.Text = empEmailMapping.SendDateTime.ToString();
            if ("incpet".Equals(this.type))
            {
                this.Label2.Text = "接收人:";
                this.lblSendUserName.Text = empEmailMapping.InceptUser.UserName;
            }
            else
            {
                this.lblSendUserName.Text = empEmailMapping.SendUser.UserName;
            }
            this.ftbEmailContent.Text = empEmailMapping.EmialTextContent;
            List<FileUploadMappingtb> affixFiles = empEmailMapping.AffixFiles;
            DataTable dt = new DataTable();
            dt.Columns.Add("affixFileName", typeof(string));
            dt.Columns.Add("affixFIlePath", typeof(string));
            foreach (FileUploadMappingtb affixFile in affixFiles)
            {
                DataRow dr = dt.NewRow();
                string fullFileName = affixFile.FilePath.Substring(affixFile.FilePath.LastIndexOf('\\') + 1);
                string fileName = fullFileName.Substring(fullFileName.IndexOf('_') + 1);
                dr["affixFileName"] = fileName;
                dr["affixFilePath"] = affixFile.FilePath.Replace(FindConfigurationPath.instance().InformFilePath, "");
                dt.Rows.Add(dr);
            }
            this.dltAffixFiles.DataSource = dt.DefaultView;
            this.dltAffixFiles.DataBind();
        }
    }
    protected void btnRetu_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("EmailInfoShow.aspx?type=" + this.type);
    }
    protected void dltAffixFiles_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string filePath = ((HtmlInputHidden)e.Item.FindControl("hidFilePath")).Value;
            string fileName = filePath.Substring(filePath.IndexOf('_') + 1);
            ((LinkButton)e.Item.FindControl("lbtDownLoad")).Attributes.Add("filePath", filePath);
            ((LinkButton)e.Item.FindControl("lbtDownLoad")).Attributes.Add("fileName", fileName);
        }
    }
    protected void lbtDownLoad_Click(object sender, EventArgs e)
    {
        string filePath = FindConfigurationPath.instance().InformFilePath + ((LinkButton)sender).Attributes["filePath"].ToString();
        string fileName = ((LinkButton)sender).Attributes["fileName"].ToString();
        FileInfo fi = new FileInfo(filePath);
        DirectoryInfo dirinfo = new DirectoryInfo(Server.MapPath("fileupload_mail"));
        if (!dirinfo.Exists)
        {
            dirinfo.Create();
        }
        FileInfo[] fis = dirinfo.GetFiles();
        if (fis != null)
        {
            for (int i = 0; i < fis.Length; i++)
            {
                fis[i].Delete();
            }
        }
        fi.CopyTo(Server.MapPath("fileupload_mail/") + fileName, true);
        Response.Redirect("fileupload_mail/" + fileName);
        //if (fi.Exists)
        //{
        //    Response.Clear();
        //    Response.Buffer = true;
        //    Response.AddHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        //    Response.AddHeader("Content-Length", fi.Length.ToString());
        //    Response.AddHeader("Content-Transfer-Encoding", "binary");
        //    Response.ContentType = "application/octet-stream";
        //    Response.Filter.Close();
        //    Response.WriteFile(fi.FullName);
        //    Response.End();
        //}
    }
}
